package tgc.edu.exam.web.controller;

import java.util.HashSet;
import java.util.List;
import java.util.UUID;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.exam.custom.AjaxResult;
import tgc.edu.exam.custom.CommonController;
import tgc.edu.exam.entity.Permission;
import tgc.edu.exam.entity.TbMenu;
import tgc.edu.exam.entity.TreeTableModel;
import tgc.edu.exam.service.MenuService;
import tgc.edu.exam.service.PermissionService;
import tgc.edu.exam.web.form.MenuForm;

@Controller
@RequestMapping(value="/menu")
public class MenuController extends CommonController<TbMenu, UUID, MenuForm>{

	@Autowired
	private MenuService service;
	
	@Autowired
	private PermissionService permissionService;
	
	@Override
	public void edit(MenuForm form, ModelMap map) throws InstantiationException, IllegalAccessException {
		TbMenu model=new TbMenu();
		UUID id = form.getId();
		String parentId = form.getParentId();
		if(id!=null) {
			model=service.findById(id);
			parentId=model.getParentId();
		}
		if(StringUtils.hasText(parentId)&&!"0".equals(parentId)) {
			TbMenu parent = service.findById(UUID.fromString(parentId));
			model.setParent(parent);
		}
		map.put("model", model);
		if(parentId!=null) {
			
			List<Permission> permissions = permissionService.findAll(Sort.by("idx1"));
			map.put("roles", permissions);
		}
	}
	@Override
	public Object save(MenuForm form) {
		try {
			
			TbMenu model=new TbMenu();
			UUID id = form.getId();
			if(id!=null) {
				model=service.findById(id);
			}
			String parentId = form.getParentId();
			if(!StringUtils.hasText(parentId)||"0".equals(parentId)) {
				model.setParent(null);
			}else {
				model.setParent(new TbMenu(UUID.fromString(parentId)));
			}
			UUID roleId = form.getRole().getId();
			if(roleId==null) {
				model.setRole(null);
			}else {
				Permission role = permissionService.findById(roleId);
				model.setRole(role);
			}
			BeanUtils.copyProperties(form, model,"id","role","parent");
			service.save(model);
			return new AjaxResult("数据保存成功");
		} catch (Exception e) {
			return new AjaxResult(false,"数据保存失败");
		}
	}
	
	@RequestMapping(value="/treeList")
	@ResponseBody
	public Object treeList() {
		Sort sort=Sort.by("idx1");
		List<TbMenu> list = service.findAll(sort);
		return new TreeTableModel(list);
	}

	@Override
	public Specification<TbMenu> buildSpec(MenuForm form) {
		@SuppressWarnings({ "serial", "unused" })
		Specification<TbMenu> spec=new Specification<TbMenu>() {

			@Override
			public Predicate toPredicate(Root<TbMenu> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules=new HashSet<>();
				Integer type = form.getType();
				if(type!=null) {
					rules.add(cb.equal(root.get("type"), type));
				}
				return cb.and(new Predicate[rules.size()]);
			}
		};
		
		return spec;
	}

}
